package com.yoomiito.app.ui.my.stock

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.yoomiito.app.Constant
import com.yoomiito.app.R
import com.yoomiito.app.adapter.ViewPagerFragmentAdapter
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.model.bean.StockBean
import com.yoomiito.app.model.bean.StockInfo
import com.yoomiito.app.utils.EventMessage
import com.yoomiito.app.utils.L
import com.zhy.view.flowlayout.TagFlowLayout.OnSelectListener
import kotlinx.android.synthetic.main.act_order.act_order_tabLayout
import kotlinx.android.synthetic.main.act_order.act_order_vp
import kotlinx.android.synthetic.main.activity_stock.tv_num
import kotlinx.android.synthetic.main.title_bar.iv_back_left
import kotlinx.android.synthetic.main.title_bar.tv_center
import org.greenrobot.eventbus.EventBus

class StockActivity : BaseActivity<StockPresenter>() {
    private var type = ACTIVITY_TYPE_REMAIN
    private val mFragments = ArrayList<Fragment>()
    private var tabSelect = 0
    private var goodsNum: MutableList<String> = ArrayList()

    override fun getLayoutId(): Int {
        return R.layout.activity_stock
    }

    override fun newP(): StockPresenter {
        return StockPresenter(App.mAppConfig)
    }

    override fun onStart() {
        super.onStart()
    }

    override fun initData(savedInstanceState: Bundle?) {
        type = intent.getIntExtra(KEY_ACTIVITY_TYPE, ACTIVITY_TYPE_REMAIN)
        tv_center.text = if (type == 1) "云库存" else "实体库存"
        initTop()
        p.goodsNum(type)


    }


    private fun initTop() {
        iv_back_left.setOnClickListener { finish() }
    }

    override fun eventMessage(eventMessage: EventMessage) {
        L.i("订单列表收到：" + eventMessage.eventType + "   " + eventMessage.goodsType)
        if ("stock" == eventMessage.eventType) { //只刷新当前界面数据

        }
    }

    fun setData(t: StockBean) {
        dismissLoading()
//        tv_num.text = "" + t.totalNum
        mFragments.clear()
        for (i in t.goodsNum.indices) {
            val orderFragment = StockFragment()
            val bundle = Bundle()
            bundle.putInt(Constant.TYPE, type)
            bundle.putString(Constant.KEY, t.goodsNum[i].productId)
            orderFragment.arguments = bundle
            mFragments.add(orderFragment)
            goodsNum.add(t.goodsNum[i].num)
        }
        tv_num.text = "" + goodsNum[0]
        act_order_vp!!.adapter = ViewPagerFragmentAdapter(supportFragmentManager, mFragments)
        for (i in t.goodsNum.indices) {
            act_order_tabLayout.addTab(
                act_order_tabLayout.newTab().setText(t.goodsNum[i].productName)
            )
        }
        act_order_tabLayout.setupWithViewPager(act_order_vp)
        for (i in t.goodsNum.indices) {
            act_order_tabLayout.getTabAt(i)?.text = t.goodsNum[i].productName
        }
        act_order_tabLayout.getTabAt(tabSelect)?.select()
        act_order_vp.addOnPageChangeListener(object : OnPageChangeListener{
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                tv_num.text = "" + goodsNum[position]
            }

            override fun onPageScrollStateChanged(state: Int) {
            }

        })

    }

    companion object {
        const val ACTIVITY_TYPE_REMAIN = 1
        const val KEY_ACTIVITY_TYPE = "key_activity_type"
    }

}
